package com.interview.ujiuye6;

import java.util.concurrent.TimeUnit;

class MyData{
    volatile int number = 0;

    public void addTo100(){
        this.number = 100;
    }
}
public class Demo1 {
    public static void main(String[] args) {
        MyData  myData = new MyData();
        //创建一个子线程：没有看到，感知到子线程的修改结果
        new Thread( () -> {
                System.out.println("--------开始执行子线程---------");
                try { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); }
                myData.number = 100;
                System.out.println("--------结束执行子线程---------");
            }
        ).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                myData.number = 100;
            }
        }).start();


        //主线程
        while(myData.number == 0){

        }
        System.out.println("main方法结束......");
    }
}
